﻿<phone:PhoneApplicationPage
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:util="clr-namespace:SkiTracker.Controls"
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
    xmlns:converter="clr-namespace:SkiTracker.Converter"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
    xmlns:command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP8"
    xmlns:behavior="clr-namespace:SkiTracker.Behavior"
    xmlns:ec="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
    x:Class="SkiTracker.View.ConversationPage"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True">
    <phone:PhoneApplicationPage.Resources>
        <toolkit:RelativeTimeConverter x:Key="RelativeTimeConverter" />
        <converter:UtcToLocalDateTimeConverter x:Key="LocalTimeConverter" />
        <converter:MessageToNavigateCommandConverter x:Key="MessageToNavigateCommandConverter" />
    </phone:PhoneApplicationPage.Resources>

    <phone:PhoneApplicationPage.FontFamily>
        <StaticResource ResourceKey="PhoneFontFamilyNormal"/>
    </phone:PhoneApplicationPage.FontFamily>
    <phone:PhoneApplicationPage.FontSize>
        <StaticResource ResourceKey="PhoneFontSizeNormal"/>
    </phone:PhoneApplicationPage.FontSize>
    <phone:PhoneApplicationPage.Foreground>
        <StaticResource ResourceKey="PhoneForegroundBrush"/>
    </phone:PhoneApplicationPage.Foreground>
    
    <phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar Mode="Default" Opacity="1.0" IsMenuEnabled="True" IsVisible="True" >
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem Click="MenuItem_Click" Text="Alles OK?" />
                <shell:ApplicationBarMenuItem Click="MenuItem_Click" Text="Hier gibt's Tiefschnee!" />
                <shell:ApplicationBarMenuItem Click="MenuItem_Click" Text="Ich brauche eine Pause!" />
                <shell:ApplicationBarMenuItem Click="MenuItem_Click" Text="Après-Ski Time?!" />
            </shell:ApplicationBar.MenuItems>
            <shell:ApplicationBarIconButton Click="Send_Click" IconUri="/Assets/AppBar/Send.png" Text="Senden" IsEnabled="False"/>
            <shell:ApplicationBarIconButton Click="SendHelp_Click" IconUri="/Assets/AppBar/Warning.png" Text="Hilfe" />
            <shell:ApplicationBarIconButton Click="Refresh_Click" IconUri="/Assets/AppBar/Refresh.png" Text="Refresh" />
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
    	<VisualStateManager.VisualStateGroups>
    		<VisualStateGroup x:Name="TextInput">
    			<VisualStateGroup.Transitions>
    				<VisualTransition GeneratedDuration="0:0:0.4">
    					<VisualTransition.GeneratedEasingFunction>
    						<CircleEase EasingMode="EaseOut"/>
    					</VisualTransition.GeneratedEasingFunction>
    				</VisualTransition>
    			</VisualStateGroup.Transitions>
    			<VisualState x:Name="Small"/>
    			<VisualState x:Name="Large">
    				<Storyboard>
    					<DoubleAnimation Duration="0" To="150" Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="textBox" d:IsOptimized="True"/>
    				</Storyboard>
    			</VisualState>
    		</VisualStateGroup>
    	</VisualStateManager.VisualStateGroups>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel Grid.Row="0" Margin="12,17,0,28">
            <TextBlock Text="Ski-Tracker" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock Text="unterhaltung" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

            <ListBox ItemsSource="{Binding SortedMessages.View}" x:Name="ListBox">
                <ListBox.Resources>
                    <converter:MessageToHorizontalAlignmentConverter x:Key="MessageToHorizontalAlignmentConverter" />
                </ListBox.Resources>
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="Padding" Value="12"/>
                        <Setter Property="HorizontalContentAlignment" Value="Left"/>
                        <Setter Property="VerticalContentAlignment" Value="Top"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="ListBoxItem">
                                    <Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}"
                                            BorderThickness="{TemplateBinding BorderThickness}" 
                                            Background="{TemplateBinding Background}" 
                                            HorizontalAlignment="{TemplateBinding HorizontalAlignment}" 
                                            VerticalAlignment="{TemplateBinding VerticalAlignment}">
                                        <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" 
                                                        HorizontalContentAlignment="{Binding Converter={StaticResource MessageToHorizontalAlignmentConverter}}"/>
                                    </Border>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </ListBox.ItemContainerStyle>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid Background="{Binding Color, Converter={StaticResource ColorToBrushConverter}}" Width="350" 
                              util:GridUtilities.RowDefinitions="*, Auto"  
                              util:GridUtilities.ColumnDefinitions="*, Auto">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Tap">
                                    <command:EventToCommand Command="{Binding Converter={StaticResource MessageToNavigateCommandConverter}}" />
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                            <TextBlock Text="{Binding Text}" TextWrapping="Wrap" Grid.ColumnSpan="2"  Margin="10" HorizontalAlignment="Stretch" FontSize="24"/>
                            <TextBlock Text="{Binding SenderName}" FontSize="18" Margin="10" HorizontalAlignment="Left" Grid.Row="1" Foreground="Linen"/>
                            <TextBlock Text="{Binding CreatedAtRelative, Converter={StaticResource RelativeTimeConverter}, ConverterCulture=de-DE}" FontSize="18" Margin="10" 
                                       HorizontalAlignment="Right" Grid.Row="1" Grid.Column="1" Foreground="Linen"/>

                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

            <TextBox x:Name="textBox" Text="{Binding Message, Mode=TwoWay}" InputScope="Text" KeyDown="TextBox_KeyDown"
                         TextWrapping="Wrap"
                         Grid.Row="1" Width="375" Height="72" Margin="0 10 0 0" 
                         HorizontalAlignment="Right">
                <i:Interaction.Behaviors>
                    <behavior:UpdateOnTextChangedBehavior/>
                </i:Interaction.Behaviors>
            	<i:Interaction.Triggers>
            		<i:EventTrigger EventName="GotFocus">
            			<ec:GoToStateAction StateName="Large"/>
            		</i:EventTrigger>
            		<i:EventTrigger EventName="LostFocus">
            			<ec:GoToStateAction StateName="Small"/>
            		</i:EventTrigger>
            	</i:Interaction.Triggers>
            </TextBox>

        </Grid>
    </Grid>

</phone:PhoneApplicationPage>